//电影管理

var express = require('express');
var router = express.Router();
var sqlQuery = require('../../../module/sql');
var fs = require('fs')
//引入上传模块
var multer = require('multer');
//上传的配置
var upload = multer({dest:"./public/upload"});


/* GET users listing. */
router.get('/', async(req, res, next)=> {
  let sid = req.query.id
  console.log(sid)
  let sqlStr = 'select * from slide where id = ?';
  let result = await sqlQuery(sqlStr,[sid])
  let slide =  result[0]
  let options = {slide}
  console.log(options)
  res.render('admin/slide/slideedit',options)
  
});



router.post('/img',upload.single('poster'), async(req, res, next)=> {
  let slideid = req.body.filmid
  let result = rename(req)
  res.json(result)
})
//改名函数
function rename(req) {
  //重命名操作
  let oldPath = req.file.destination+"/"+req.file.filename;
  let newPath = req.file.destination+"/"+req.file.filename+req.file.originalname;
  fs.rename(oldPath,newPath,()=>{
    //console.log("重命名成功")
  })
  return {
    state:'ok',
    imgUrl:"/upload/"+req.file.filename+req.file.originalname
  }
 }


router.post('/', async(req, res)=>{
  let slideName = req.body.SlideName
  let img = req.body.img_h
  let sid = req.body.fid
  let sqlStr3 = "update slide set slideName=?, routeImg=? where id = ?"
  let arr = [slideName,img,sid]
  await sqlQuery(sqlStr3,arr)
  res.json({
    state:"ok",
    content:"轮播图更新成功"
  })

})


module.exports = router;
